/*
 * Copyright (C) 2010 Denis Mingulov.
 *
 * This file is part of QReader QtWebView test.
 *
 * Author: Denis Mingulov <denis@mingulov.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "qreadernetworkreply.h"
#include <QTimer>
#include <QFile>

QReaderNetworkReply::QReaderNetworkReply(const QNetworkRequest &request, const QUrl &url)
{
    setRequest( request );
    setOpenMode( QIODevice::ReadOnly );

    m_data = loadResource( url );
    m_length = m_data.length();

    setHeader( QNetworkRequest::ContentLengthHeader, QByteArray::number(m_data.length()) );
    QTimer::singleShot( 0, this, SIGNAL(metaDataChanged()) );
    QTimer::singleShot( 0, this, SIGNAL(readyRead()) );
}

bool QReaderNetworkReply::isSequential() const
{
    return true;
}

void QReaderNetworkReply::abort()
{
}

qint64 QReaderNetworkReply::readData(char *buffer, qint64 maxlen)
{
    qint64 len = qMin(qint64(m_data.length()), maxlen);
    if (len)
    {
        qMemCopy(buffer, m_data.constData(), len);
        m_data.remove(0, len);
    }

    if (!m_data.length())
        QTimer::singleShot(0, this, SIGNAL(finished()));
    return len;
}

qint64 QReaderNetworkReply::bytesAvailable() const
{
    return m_data.length() + QNetworkReply::bytesAvailable();
}

QByteArray QReaderNetworkReply::loadResource( const QUrl &url )
{
    QString data, file, path = url.path();
    QString prefixLocal("/disk/");
    QString prefixPrivate("/internal/");

    if(path.startsWith(prefixLocal, Qt::CaseInsensitive))
    {
        file = path.mid(prefixLocal.length());
    }
    else if(path.startsWith(prefixPrivate, Qt::CaseInsensitive))
    {
        file = path.mid(prefixPrivate.length());
    }

    if(path.endsWith(".png", Qt::CaseInsensitive))
        setHeader( QNetworkRequest::ContentTypeHeader, "image/png" );
    else if(path.endsWith(".jpg", Qt::CaseInsensitive))
        setHeader( QNetworkRequest::ContentTypeHeader, "image/jpeg" );
    else if(path.endsWith(".html", Qt::CaseInsensitive) || path.endsWith(".htm", Qt::CaseInsensitive))
        setHeader( QNetworkRequest::ContentTypeHeader, "text/html" );
    else
        setHeader( QNetworkRequest::ContentTypeHeader, "binary/octet" );

    QFile f(file);
    if (!f.open(QIODevice::ReadOnly))
        return QByteArray();

    QByteArray b = f.readAll();
    f.close();
    return b;
}
